package com.ruoyi.admin.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.admin.domain.Material;
import com.ruoyi.admin.domain.Question;
import com.ruoyi.admin.domain.QuestionAnswer;
import com.ruoyi.admin.domain.QuestionOption;
import com.ruoyi.admin.mapper.QuestionMapper;
import com.ruoyi.admin.model.request.QuestionReq;
import com.ruoyi.admin.model.response.QuestionInfoResp;
import com.ruoyi.admin.service.MaterialService;
import com.ruoyi.admin.service.QuestionAnswerService;
import com.ruoyi.admin.service.QuestionOptionService;
import com.ruoyi.admin.service.QuestionService;
import com.ruoyi.common.utils.SecurityUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * (Question)表服务实现类
 *
 * @author makejava
 * @since 2023-03-27 23:13:51
 */
@Service("questionService")
public class QuestionServiceImpl extends ServiceImpl<QuestionMapper, Question> implements QuestionService {

    @Autowired
    private MaterialService materialService;

    @Autowired
    private QuestionOptionService questionOptionService;

    @Autowired
    private QuestionAnswerService questionAnswerService;

    @Override
    public boolean saveQuestion(QuestionReq question) {

        Question questionEntity = new Question();
        questionEntity.setQuestion(question.getQuestion());
        questionEntity.setQuestionType(question.getQuestionType());
        questionEntity.setCreateBy(SecurityUtils.getUsername());
        questionEntity.setCreateTime(new Date());
        questionEntity.setDelFlag(0);
        questionEntity.setTopicId(question.getTopicId());
        questionEntity.setModuleType(question.getModuleType());
        questionEntity.setPaperId(question.getPaperId());
        boolean save = this.save(questionEntity);

        QuestionAnswer questionAnswer = new QuestionAnswer();
        questionAnswer.setAnswer(question.getAnswer());
        questionAnswer.setQuestionId(questionEntity.getId());
        questionAnswer.setCreateBy(SecurityUtils.getUsername());
        questionAnswer.setCreateTime(new Date());
        questionAnswer.setDelFlag(0);
        questionAnswer.setPaperId(question.getPaperId());
        save = questionAnswerService.save(questionAnswer);

        if (Objects.equals(question.getQuestionType(), 0)
                && CollectionUtils.isNotEmpty(question.getOptionList())) {
            List<QuestionOption> questionOptionList = question.getOptionList().stream().map(m -> {
                QuestionOption questionOption = new QuestionOption();
                questionOption.setOptionContent(m);
                questionOption.setQuestionId(questionEntity.getId());
                questionOption.setCreateBy(SecurityUtils.getUsername());
                questionOption.setCreateTime(new Date());
                questionOption.setDelFlag(0);
                questionOption.setPaperId(question.getPaperId());
                return questionOption;
            }).collect(Collectors.toList());
            save = questionOptionService.saveBatch(questionOptionList);

        }

        if (ObjectUtils.isNotEmpty(question.getMaterialType())) {
            Material materialEntity = new Material();
            materialEntity.setMaterialType(question.getMaterialType());
            materialEntity.setMaterialUrl(question.getMaterialUrl());
            materialEntity.setMaterialContent(question.getMaterialContent());
            materialEntity.setQuestionId(questionEntity.getId());
            materialEntity.setCreateBy(SecurityUtils.getUsername());
            materialEntity.setCreateTime(new Date());
            materialEntity.setDelFlag(0);
            materialEntity.setPaperId(question.getPaperId());
            save = materialService.save(materialEntity);
        }
        return save;
    }

    @Override
    public List<QuestionInfoResp> questionPage(Page page, Question questionReq) {
        Page<Question> questionPage = this.page(page, new LambdaQueryWrapper<Question>(questionReq));
        return buildQuestionResp(questionPage.getRecords());
    }

    public List<QuestionInfoResp> questionList(Question questionReq){
        List<Question> questionList = this.list(new LambdaQueryWrapper<>(questionReq));
        return buildQuestionResp(questionList);
    }

    public List<QuestionInfoResp> buildQuestionResp(List<Question> questionList){
        List<Long> questionIdList = questionList.stream().map(Question::getId).collect(Collectors.toList());
        // 获取所有的答案
        List<QuestionAnswer> questionAnswerList = questionAnswerService.list(new LambdaQueryWrapper<QuestionAnswer>()
                .in(QuestionAnswer::getQuestionId, questionIdList)
        );

        // 获取所有选项
        List<QuestionOption> questionOptionList = questionOptionService.list(new LambdaQueryWrapper<QuestionOption>()
                .in(QuestionOption::getQuestionId, questionIdList));

        // 获取所有材料
        List<Material> materialList = materialService.list(new LambdaQueryWrapper<Material>()
                .in(Material::getQuestionId, questionIdList));

        List<QuestionInfoResp> questionPageList = questionList.stream().map(question -> {
            QuestionInfoResp questionInfoResp = new QuestionInfoResp();
            questionInfoResp.setQuestionId(question.getId());
            questionInfoResp.setQuestion(question.getQuestion());
            questionInfoResp.setQuestionType(question.getQuestionType());
            questionInfoResp.setTopicId(question.getTopicId());

            if (SecurityUtils.getUserType() == 0) {
                questionAnswerList.stream().filter(questionAnswer ->
                                questionAnswer.getQuestionId().equals(question.getId()))
                        .findFirst().ifPresent(questionAnswer ->
                                questionInfoResp.setAnswer(questionAnswer.getAnswer()
                        ));
            }

            if (Objects.equals(question.getQuestionType(), 0)) {
                List<String> optionList = questionOptionList.stream().filter(questionOption ->
                        questionOption.getQuestionId().equals(question.getId())
                ).sorted(Comparator.comparing(QuestionOption::getId))
                        .map(QuestionOption::getOptionContent).collect(Collectors.toList());
                questionInfoResp.setOptionList(optionList);
            }

            materialList.stream().filter(material ->
                            material.getQuestionId().equals(question.getId()))
                    .findFirst().ifPresent(material -> {
                        questionInfoResp.setMaterialUrl(material.getMaterialUrl());
                        questionInfoResp.setMaterialType(material.getMaterialType());
                        questionInfoResp.setMaterialContent(material.getMaterialContent());
            });
            return questionInfoResp;
        }).collect(Collectors.toList());
        return questionPageList;
    }


}

